package com.ita.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class DBUtil {

	private static final String DRIVER_NAME;
	private static final String URL;
	private static final String USER_NAME;
	private static final String PASSWORD;
	
	
	private static DataSource jndiDs;
	private static BasicDataSource ds;

	
	static {
		Properties properties = PropertiesUtil.getProp();
		DRIVER_NAME = properties.getProperty("DRIVER_NAME");
		URL = properties.getProperty("URL");
		USER_NAME = properties.getProperty("USER_NAME");
		PASSWORD = properties.getProperty("PASSWORD");
		
		
		ds = new BasicDataSource();
		ds.setUrl(URL);
		ds.setDriverClassName(DRIVER_NAME);
		ds.setUsername(USER_NAME);
		ds.setPassword(PASSWORD);
	
	}

	public static Connection createConnection() {

		Connection conn = null;
		try {
			Class.forName(DRIVER_NAME);
			conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return conn;
	}

	public static Connection createByDs() {

		Connection conn = null;
		try {
			return ds.getConnection();
		} catch (SQLException e) {
		}
		return conn;
	}


	public static void close(Connection conn, PreparedStatement pst,
			ResultSet rSet) {

		try {
			if (conn != null) {
				conn.close();
			}
			if (pst != null) {
				pst.close();
			}
			if (rSet != null) {
				rSet.close();
			}
		} catch (Exception e) {
		}
	}

}
